package chapter1.No1_7_停止线程.c能停止的线程_异常法_重点;

/**
 * *：中断线程之异常法
 *    主要是用抛出异常，让程序不往下面执行。从而达到及时停止的目的！
 */
public class T1_MyThread extends Thread {
    @Override
    public void run() {
        try {
            for (int i = 0; i < 50000; i++) {
                if (this.isInterrupted()) {
                    System.out.println("线程已经被中断，我要退出了！");
                    throw new InterruptedException();
                }
                System.out.println("i=" + i);
            }
            System.out.println("我是for后面的业务代码~");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        T1_MyThread m1 = new T1_MyThread();
        m1.start();
        Thread.sleep(100);
        m1.interrupt();
        System.out.println("END");
    }
}
